

public class BinarySearchTree {
    static class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val) {
            this.val = val;
        }

    }
    TreeNode root = null;

    /**
     * 查找二叉树搜索树中指定的val值
     * 定义cur 从root开始遍历
     * 如果cur的值小于 要查找的val , cur往右子树走
     * 如果 cur的值等于 val值 , 返回cur
     * 如果 cur的值大于val , cur往左子树走
     * @param val
     * @return
     */
    public TreeNode find(int val) {
        TreeNode cur = root;
        while (cur != null) {
            if (cur.val < val) {
                cur = cur.right;
            }else if (cur.val == val) {
                return cur;
            }else {
                cur = cur.left;
            }
        }
        return null;
    }

    /**
     * 插入一个数据
     * 当根节点为空时, 直接插入, 将val放进去
     * 如果树不空时 , 定义parent 为插入位置 , 按照查找的逻辑找到parent
     *      如果要插入元素树中已经有了 则直接返回
     *      如果插入的元素大于 cur的值, 将cur给parent , cur向右子树走
     *      如果插入的元素小于 cur的值, 将cur给parent , cur往左子树走
     * 找到parent 即插入位置后 , 插入元素
     *      如果parent的值 小于要插入元素的值 , 将插入元素放到parent的右子树
     *      反之 , 放到parent 的左子树
     * @param val
     */
    public  void insert(int val) {
        if (root == null) {
            root = new TreeNode(val);
            return;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (val == cur.val) {
                return;
            }else if (val > cur.val){
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
        TreeNode node = new TreeNode(val);
        if (parent.val < val) {
            parent.right = node;
        }else {
            parent.left = node;
        }
    }
    public void inorder(TreeNode root) {
        if (root == null) {
            return;
        }
        inorder(root.left);
        System.out.print(root.val + " ");
        inorder(root.right);
    }

    /**
     * 删除一个数据
     *
     * @param val
     */
    /**
     * 1.找到删除位置 parent
     * @param val
     */
    public void remove(int val) {
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if(cur.val == val) {
                removeNode(parent,cur);
                return;
            }else if(cur.val < val) {
                parent = cur;
                cur = cur.right;
            }else {
                parent = cur;
                cur = cur.left;
            }
        }
    }

    /**
     * 删除操作:
     *
     * @param parent
     * @param cur
     */
    private void removeNode(TreeNode parent, TreeNode cur) {
        if (cur.left == null) {
            if (cur == root) {
                root = cur.right;
            }else if (parent.left == cur) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }else if (cur.right == null) {
            if (cur == root) {
                root = cur.left;
            }else if (parent.left == cur) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while (target.left != null) {
                targetParent = target;
                target = target.left;

            }
            cur.val = target.val;
            if (target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }
}
